package _17_剑指Offer;

import _17_剑指Offer.datastrcute.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class _32_剑指Offer从上到下打印二叉树 {

    public int[] levelOrder(TreeNode root) {
        if (root == null) return new int[0];

        List<Integer> results = new ArrayList<>();
        // 二叉树层序遍历
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            results.add(node.val);
            if (node.left != null) queue.offer(node.left);

            if (node.right != null) queue.offer(node.right);
        }
        int len = results.size();
        int[] ins = new int[len];
        for (int i = 0; i < len; i++) {
            ins[i] = results.get(i);
        }
        return ins;
    }


}
